package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0216.java
 * @Author : WangRuoyu
 * @Date 2023/11/1 20:06
 */

import java.util.ArrayList;
import java.util.List;

public class Q0216 {
    int k;
    int n;
    List<Integer> tmp = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();

    private void dfs(int x, int sum) {
        if (tmp.size() == k) {
            if (n == sum) {
                res.add(new ArrayList<>(tmp));
            }
            return;
        }
        for (int i = x; i <= 9; i++) {
            if (i > n - sum) return;
            tmp.add(i);
            dfs(i + 1, sum + i);
            tmp.remove(tmp.size() - 1);
        }
    }

    public List<List<Integer>> combinationSum3(int k, int n) {
        this.k = k;
        this.n = n;
        dfs(1, 0);
        return res;
    }

    public static void main(String[] args) {
        Q0216 sl = new Q0216();
        int k = 4;
        int n = 10;
        System.out.println(sl.combinationSum3(k, n));
    }
}